package org.behrang.macbeans.tab.camino;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.plaf.ComponentUI;
import org.netbeans.swing.tabcontrol.TabDisplayer;
import org.netbeans.swing.tabcontrol.plaf.AquaEditorTabDisplayerUI;
import org.netbeans.swing.tabcontrol.plaf.TabCellRenderer;
import org.netbeans.swing.tabcontrol.plaf.TabControlButton;
import org.netbeans.swing.tabcontrol.plaf.TabControlButtonFactory;

/**
 *
 * @author behrangsa
 */
public class CaminoEditorTabDisplayerUI extends AquaEditorTabDisplayerUI implements CaminoColors{

    public static final int PREFERRED_HEIGHT = 25;
    public static final int MINIMUM_X_POSITION = 7;

    private static Map<Integer, String[]> buttonIconPaths;
    
    private JPanel controlButtons;
    
    private TabControlButton btnScrollLeft;
    private TabControlButton btnScrollRight;
    private TabControlButton btnDropDown;
    private TabControlButton btnMaximizeRestore;
    
    public CaminoEditorTabDisplayerUI(TabDisplayer displayer) {
        super(displayer);
    }

    @Override
    public void install() {
        super.install();
        scroll().setMinimumXposition(MINIMUM_X_POSITION);
    }

    @Override
    protected TabCellRenderer createDefaultRenderer() {
        return new CaminoEditorTabCellRenderer();
    }

    public static ComponentUI createUI(JComponent c) {
        return new CaminoEditorTabDisplayerUI((TabDisplayer) c);
    }

    @Override
    protected void paintAfterTabs(Graphics g) {

        int rightLineStart = getTabsAreaWidth() - MINIMUM_X_POSITION - 1;
        int rightLineEnd = displayer.getWidth();

        if (displayer.getModel().size() > 0 && !scroll().isLastTabClipped()) {
            int idx = scroll().getLastVisibleTab(displayer.getWidth());
            rightLineStart = scroll().getX(idx) + scroll().getW(idx);
        } else if (displayer.getModel().size() == 0) {
            rightLineStart = 6;
        }
        
        int y = displayer.getY() + 1;     
        int h = displayer.getHeight() - 1;
        
        // upper line
        g.setColor(UL);
        g.drawLine(rightLineStart, y, rightLineEnd, y);

        // background
        g.setColor(BG);
        g.fillRect(rightLineStart, y + 1, (rightLineEnd - rightLineStart), h - 7);

        // bottom lines
        g.setColor(B1);
        g.drawLine(rightLineStart + 1, h - 6, rightLineEnd, h - 6);
        g.setColor(B2);
        g.drawLine(rightLineStart, h - 5, rightLineEnd, h - 5);
        g.setColor(B3);
        g.drawLine(rightLineStart, h - 4, rightLineEnd, h - 4);
        g.setColor(B4);
        g.drawLine(rightLineStart, h - 3, rightLineEnd, h - 3);
        g.setColor(B5);
        g.drawLine(rightLineStart, h - 2, rightLineEnd, h - 2);

//        if (scroll().getOffset() >= 0) {
//            //fill the left edge notch
//            g.drawLine(6, 0, 11, 0);
//        }
//                
//        
//        Utils.prints("Disp.Height", displayer.getHeight());
//
//        final Color old = g.getColor();       
//        
//        g.setColor(new Color(174, 174, 174));
//        g.drawLine(rightLineStart, 0, rightLineEnd, 0);
//        g.setColor(new Color(64, 64, 64));
//        g.drawLine(rightLineStart, 1, rightLineEnd, 1);
//        g.drawLine(rightLineStart, 22, rightLineEnd, 22);
//        g.setColor(new Color(153, 153, 153));
//        g.drawLine(rightLineStart, 21, rightLineEnd, 21);
//        
//        g.setColor(old);
    }

    /**
     * Overridden to adjust the visible height of the tab displayer area.
     * 
     * @param c
     * @return
     */
    @Override
    public Dimension getPreferredSize(JComponent c) {
        Dimension ps = super.getPreferredSize(c);
        ps.height = PREFERRED_HEIGHT;
        return ps;
    }

    /**
     * Overridden to adjust the height of the rendering bound of tabs.
     * 
     * @param tabIndex
     * @param bounds
     * @return
     */
    @Override
    public Rectangle getTabRect(int tabIndex, Rectangle bounds) {
        Rectangle tabRect = super.getTabRect(tabIndex, bounds);
        tabRect.height = PREFERRED_HEIGHT - 2;
        return tabRect;
    }
    
    private static void initIcons() {
        if( null == buttonIconPaths ) {
            buttonIconPaths = new HashMap<Integer, String[]>(7);
            
//            //left button
            String[] iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/tab_scroll_button_left.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/tab_scroll_button_left.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/tab_scroll_button_left.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/tab_scroll_button_left.png"; // NOI18N
            buttonIconPaths.put( TabControlButton.ID_SCROLL_LEFT_BUTTON, iconPaths );
            
//            //right button
            iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/tab_scroll_button_right.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/tab_scroll_button_right.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/tab_scroll_button_right.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/tab_scroll_button_right.png"; // NOI18N
            buttonIconPaths.put( TabControlButton.ID_SCROLL_RIGHT_BUTTON, iconPaths );
            
            //drop down button  
            iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/tab_menu_button.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/tab_menu_button.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/tab_menu_button.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/tab_menu_button.png"; // NOI18N
            buttonIconPaths.put( TabControlButton.ID_DROP_DOWN_BUTTON, iconPaths );
            
            //maximize/restore button
            iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/green.png"; // NOI18N
            buttonIconPaths.put( TabControlButton.ID_MAXIMIZE_BUTTON, iconPaths );
            
            iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/green.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/green.png"; // NOI18N
            buttonIconPaths.put( TabControlButton.ID_RESTORE_BUTTON, iconPaths );
        }
    }

    public Icon getButtonIcon(int buttonId, int buttonState) {                
        Icon res = null;
        initIcons();
        String[] paths = buttonIconPaths.get( buttonId );
        if( null != paths && buttonState >=0 && buttonState < paths.length ) {
            // res = new CaminoTabControlIcon(getClass().getResource(paths[0]));
            res = new ImageIcon(getClass().getResource(paths[0]));
        }
        return res;
    }

    @Override
    protected Rectangle getControlButtonsRectangle(Container parent) {
        Component c = getControlButtons();
        Rectangle rect = new Rectangle( parent.getWidth()-c.getWidth(), 0, c.getWidth(), c.getHeight() );        
        return rect;
    }
    
    /**
     * @return A component that holds control buttons (scroll left/right, drop down menu)
     * that are displayed to right of the tab area.
     */
    protected Component getControlButtons() {
        if( null == controlButtons ) {
            JPanel buttonsPanel = new JPanel( null );
            buttonsPanel.setOpaque( false );

            int width = 0;
            int height = 0;

            //create scroll-left button
            Action a = scroll().getBackwardAction();
            a.putValue( "control", displayer ); //NO18N
            btnScrollLeft = TabControlButtonFactory.createScrollLeftButton( displayer, a, false );
            buttonsPanel.add( btnScrollLeft );
            Dimension prefDim = btnScrollLeft.getPreferredSize();            
            btnScrollLeft.setBounds(width, 0, 18, prefDim.height );
            width += 18;
            height = prefDim.height;

            //create scroll-right button
            a = scroll().getForwardAction();
            a.putValue( "control", displayer ); //NO18N
            btnScrollRight = TabControlButtonFactory.createScrollRightButton( displayer, a, false );
            buttonsPanel.add( btnScrollRight );
            prefDim = btnScrollRight.getPreferredSize();            
            btnScrollRight.setBounds(width, 0, 18, prefDim.height );
            width += 18;
            height = Math.max ( height, prefDim.height );

            //create drop down button
            btnDropDown = TabControlButtonFactory.createDropDownButton( displayer, false );
            buttonsPanel.add( btnDropDown );            
            prefDim = btnDropDown.getPreferredSize();            
            btnDropDown.setBounds(width, 0, 18, prefDim.height );
            width += 18;
            height = Math.max ( height, prefDim.height );
            
            //maximize / restore button
            if( null != displayer.getWinsysInfo() ) {                
                btnMaximizeRestore = TabControlButtonFactory.createMaximizeRestoreButton( displayer, false );
                buttonsPanel.add( btnMaximizeRestore );
                prefDim = btnMaximizeRestore.getPreferredSize();                
                btnMaximizeRestore.setBounds(width, 0, 18, prefDim.height);
                width += 18;
                height = Math.max ( height, prefDim.height );
            }
            
            height += 1;
            
            Dimension size = new Dimension( width, height );
            buttonsPanel.setMinimumSize( size );
            buttonsPanel.setSize( size );
            buttonsPanel.setPreferredSize( size );
            buttonsPanel.setMaximumSize( size );
            
            controlButtons = buttonsPanel;
            
        }
        
        return controlButtons;
    }
    
}
